package club.ljwang.service;

import club.ljwang.dao.CheckItemMapper;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.exception.CheckItemException;
import club.ljwang.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: wangl
 * @Date: 2020/2/24 17:06
 * @Description:
 */

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.告诉Mybatis我们要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2.写一个跟分页毫无关系的sql
        List<CheckItem> checkItems = checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),checkItems);
    }

    @Override
    public void delete(Integer id) throws CheckItemException{
        Integer count = checkItemMapper.findCountByCheckItemId(id);
        if (count>0) {

            throw new CheckItemException("检查组存在此检查项引用,引用ID为"+id);

        }else {

            checkItemMapper.delete(id);

        }

    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemMapper.findById(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {

        return checkItemMapper.findAll();
    }
}
